package com.woan.api.servlet;


import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysRole;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;
import com.woan.api.services.UserClientServices;
import com.woan.api.services.VideoClientServices;
import com.woan.api.util.FuncDate;
import com.woan.api.util.HttpTool;

import net.sf.json.JSONObject;

public class VideoMu extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpTool ht = new HttpTool();
	
	protected FuncDate fd = null;

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		//Hashtable<String, String> hash = null;
		//hash = this.ht.getRequestBody(request);
		
			//System.out.println("视频服务器发送的内容---------"+hash);
		
			String str1 = request.getParameter("type");
			System.err.println("m3u8 type值                                              "+str1);
			//String str1 = (String) hash.get("type");
			//如果是m3u8则需要取到type=7，这里7的通知是循环发送切片的，所以只处理通知7里面sequence=0的内容就可以	
			//直播开始
			if(str1.equals("0"))
			{		
				//如果是m3u8，直播开始的时候传入状态status=2，这个时候还不能播放，等待通知7以后才可以播放，所以存入其他的status
				videoMUStart(request, response);	
			}
		/*	//m3u8可以开始播放
			if(str1.equals("7"))
			{
				//首先获取录制的视频格式
				//String sequence =(String)hash.get("sequence");
				String sequence =request.getParameter("sequence");
				System.out.println("m3u8切片---------"+sequence);
				if(sequence.equals("0"))
				{
					videoMUReady(request, response);
				}
			}*/
//			if (str1.equals("1"))
//			{	
//				videoStop(request, response);
//			}
				
			if (str1.equals("2"))
			{
				videoArchive(request, response);
			}
			
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
	}

	public void videoMUReady(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String msg = "-1";
		System.out.println("----------视频M3U8直播开始-----------");
		//System.out.println("----------视频开始消息内容-----------" + hash);
		//String vid = (String) hash.get("hashed_id");
		String vid = request.getParameter("hashed_id");
		System.err.println("m3u8 视频vid............" + vid);
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
		SysVideo sysVideo = videoClientServices.listOneVideoinfo(vid, "ios");
		SysVideoBasic sysVideoBasic = videoClientServices.getBasicVideoByVid(vid);
		SysRole sysRole = userClientServices.findRoleByRid(sysVideoBasic.getRid());
		sysVideo.setStatus(0);
		videoClientServices.updateVideo(sysVideo,sysVideoBasic.getRid(),sysRole.getRoleName(),sysVideoBasic.getUserName());
		videoClientServices.pushMessageM3u8(sysVideo,sysVideoBasic.getRid(),sysRole.getRoleName(),sysVideoBasic.getUserName());
		System.out.println("M3U8直播更新视频");
	}
	public void videoMUStart(HttpServletRequest request,
			HttpServletResponse response)  {
		try {
			//System.out.println("----------视频开始消息内容-----------" + hash);
			//String vid = (String) hash.get("hashed_id");
			String vid = request.getParameter("hashed_id");
			String user_name = request.getParameter("author");
			System.out.println("----------视频拍摄人是：》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》"+user_name);		
			//String user_name = (String) hash.get("author");
			//String videoPath = (String) hash.get("url");
			String videoPath = request.getParameter("url");
			//String videoSize = (String) hash.get("size");
			String videoSize = request.getParameter("size");
			String groupname = "";
			String username = "";
			if (user_name.contains("|")) 
			{
				String para[] = user_name.split("\\|");
				groupname = para[0];
				username = para[1];
			}
			ServletContext servletContext = this.getServletContext();     
	        WebApplicationContext wac = null;     
	        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
	        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
	        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
	        SysAdmininfo user = null;
	        user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
			String uid = "";
			if (user != null) {
				uid = user.getUid();
				videoClientServices.updateVideoState(uid,vid);
			} 
			SysVideo sysVideo1 = videoClientServices.listOneVideoinfo(vid, "ios");	
			if(sysVideo1==null)
			{
				SysVideo sysVideo = new SysVideo();
				sysVideo.setVid(vid);
				sysVideo.setBeginTime(FuncDate.FindCurSysTime1());
				sysVideo.setEndTime("");
				sysVideo.setPicture("");
				sysVideo.setStatus(0);
				sysVideo.setVideoLength("");
				sysVideo.setVideoPath(videoPath);
				sysVideo.setVideoSize(videoSize);
				sysVideo.setVideoType("ios");
				sysVideo.setIsdel("1");		
				System.out.println("m3u8直播插入视频");
				videoClientServices.insertVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public void videoStop(HttpServletRequest request,
			HttpServletResponse response,Hashtable<String, String> hash) throws ServletException, IOException {
//		System.out.println("---------------- 视频M3u8停止通知 -----------");
//		System.out.println("----------this.hash-----------" + this.hash);
//		String video_code = (String) this.hash.get("hashed_id");
//		String curtime = FuncDate.FindCurSysTime1();
//		VOESysVideo video = new VOESysVideo();
//		BOQVideo boqvideo = new BOQVideo();
//		video = boqvideo.FindvideoByHashid(video_code);
//		video.setVid(video_code);
//		video.setEndtime(curtime);
//		
//		video.setStatusM3u8("1");
//		BLVideo blvideo = new BLVideo();
//		String resu = "0";
//		resu = blvideo.updateVideo(video);
//		String msg = "";
//		if (resu.equals("0")) {
//			msg = "0";
//			this.ht.retData(response, msg);
//		} else {
//			msg = "-1";
//			this.ht.retData(response, msg);
//		}
	}

	public void videoArchive(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("---------------- M3u8存档通知 -----------");
		//System.out.println("----------M3u8存档通知 this.hash-----------" + hash);
		//String vid = (String) hash.get("hashed_id");
		String vid = request.getParameter("hashed_id");
		String task_id = request.getParameter("service_code");
		//获取指派任务的Id
		//String task_id=(String)hash.get("service_code");
		//String videoLength = (String) hash.get("duration");	
		String videoLength = request.getParameter("duration");
		//String videoPath = (String) hash.get("url");
		String curtime = FuncDate.FindCurSysTime1();
		String videoPath = request.getParameter("url");
		String user_name = request.getParameter("author");
		//String user_name = (String) hash.get("author");
		String groupname = "";
		String username = "";
		if (user_name.contains("|")) 
		{
			String para[] = user_name.split("\\|");
			groupname = para[0];
			username = para[1];
		}
		ServletContext servletContext = this.getServletContext();     
        WebApplicationContext wac = null;     
        wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);    
        UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
        VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
        SysAdmininfo user = null;
        System.out.println("++++++++++++++++++++++++更新任务了开始+++++++++++++++++++++++++++++++++++++++++++");
        if (!"XZB".equals(task_id)) {
        	System.out.println("++++++++++++++++++++++++更新任务iso了开始进去了+++ios++++++++++++++++++++++++++++++++++++++++++++");
        	//SysVideoBasic videoBasic_task = videoClientServices.getSysvideobasiciDAO().get(task_id);
        	//SysVideoBasic videoBasic = videoClientServices.getSysvideobasiciDAO().get(vid);
        	//String title = videoBasic.getTitle();
        	//String detail = videoBasic.getDetail();
        	//videoBasic.setDetail(detail);
        	//videoBasic.setTitle(title);
        	//把任务标题更新到新增的视频中
        	//videoClientServices.getSysvideobasiciDAO().UpdateVideoBasic(videoBasic);
        	//删除之前的任务。
        	//videoClientServices.getSysvideobasiciDAO().deletVideoBasic(videoBasic_task);
        	//根据任务id获取视频播放对象
        	//SysVideo video_task = videoClientServices.getSysvideoiDAO().FindVideoByVidAndType(task_id, "ios");
        	//videoClientServices.getSysvideoiDAO().deletVideo(video_task);
        	System.out.println("++++++++++++++++++++++++更新任务结束了+++++++++++++++++++++++++++++++++++++++++++");
		}
        System.out.println("++++++++++++++++++++++++更新任务结束了出来了+++++++++++++++++++++++++++++++++++++++++++");
          
        user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
		SysVideo sysVideo = videoClientServices.listOneVideoinfo(vid, "ios");	
		if (sysVideo!=null) {
			sysVideo.setVideoPath(videoPath);
			sysVideo.setPicture(videoPath+".jpg");
			sysVideo.setStatus(1);
			sysVideo.setEndTime(FuncDate.FindCurSysTime1());
			sysVideo.setVideoLength(videoLength);
			videoClientServices.updateVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
			System.out.println("m3u8存档更新视频");
		}  
	
	}
}